package com.jiangruan.javaPro.net;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @author:冷风诉离歌
 * @desc:该类用于封装提供网络请求的功能代码，用于发起网络请求，并将网络
 * 的数据进行返回
 * @datetime: 2021/12/13 19:40
 */

public class HttpUtils {

    public static String get(String path){
        BufferedReader reader = null;
        InputStreamReader inputStreamReader = null;
        InputStream inputStream = null;

        String line = "";
        String result = "";
        try {
            //Union Resource Location  统一资源定位
            URL url = new URL(path);
            URLConnection connection = url.openConnection();
            //进行连接
            connection.connect();

            inputStream = connection.getInputStream();

            //字符流
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
        //    String line = reader.readLine();
        //    while(line !=null){
        //        reader.readLine();
        //    }
            while ((line = reader.readLine()) !=null){
                result += line;
            }

        } catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭读取流
            try {
                if (reader != null) {
                    reader.close();
            }
                if (inputStreamReader != null){
                    inputStreamReader.close();
                }
                if (inputStream != null){
                    inputStream.close();
                }
            } catch(IOException e){
                e.printStackTrace();
            }
        }
        return result;
    }

//封装JAVA原声实现post请求，并返回数据结果
   public static String post(String path, Map<String,String> header,byte[] data) {
       OutputStream outputStream = null;
       InputStream inputStream = null;
       BufferedReader reader = null;
       String result = "";
        try {
           URL url = new URL(path);
           HttpURLConnection conn = (HttpURLConnection) url.openConnection();
           //设置可以发送数据的接受数据
           conn.setDoInput(true);
           conn.setDoOutput(true);
           //请求头信息设置
           if (!header.isEmpty()){
               Set<String> keys = header.keySet();
               for(String key :keys){
                   String value = header.get(key);
                   conn.setRequestProperty(key,value);
                   conn.setRequestProperty(key,value);
               }
           }

           conn.setRequestMethod("POST");

           conn.connect();


           //将请求的数据发送出去
            outputStream = conn.getOutputStream();
            outputStream.write(data);
           //接受返回的响应数据
             inputStream = conn.getInputStream();
             reader = new BufferedReader(new InputStreamReader(inputStream));
             String line =null;
             while ((reader.readLine()) != null){
                 result += line;
            }

       } catch (MalformedURLException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }finally {
            {
                try {
                    if (reader!=null){
                        reader.close();
                    }
                    if (inputStream!=null){
                        inputStream.close();
                    }
                    if (outputStream!=null){
                        outputStream.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return result;
        
   }
}
